<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html lang="zh">

<head>
	<meta http-equiv="content-type" content="text/html;charset=utf-8">
	<link rel="stylesheet" href="/SimpleBlog/css/default.css" type="text/css" />
	<link rel="alternate" type="application/rss+xml"
		title="小博客 - RSS 2.0"
		href="http://<?php echo $_SERVER["HTTP_HOST"]; ?>/SimpleBlog/feeds/rss.php" />
	<title>小博客</title>
</head>

<body>

<h1>小博客应用</h1>

<?php session_start();?>
<ul id="menu">
	<li><a href="/SimpleBlog/blog/">文章</a></li>
	<li><a href="/SimpleBlog/about/">关于作者</a></li>
	<?php if(!(isset($_SESSION['loggedin']) && $_SESSION['loggedin']==1)): ?>
		<li><a href="/SimpleBlog/admin">登录</a></li>
	<?php endif; ?>
</ul>
<?php if(isset($_SESSION['loggedin']) && $_SESSION['loggedin']==1): ?>
	<p id="control_panel">
		您已经登录
	<a href="/SimpleBlog/inc/update.inc.php?action=logout">注销</a>.
	</p>
<?php endif; ?>

<div id="entries">
<?php

require_once './inc/db.inc.php';
require_once './inc/functions.inc.php';
require_once './inc/comments.inc.php';

try {
	$link = new PDO(DB_INFO, DB_USERNAME, DB_PASSWORD);
} catch (PDOException $e) {
	die("数据库连接失败！".$e->getMessage()."<br />");
}

// 获取要显示的页面名和 ID 号
$url = isset($_GET["url"]) ? $_GET["url"] : NULL;
$page = isset($_GET["page"]) ? htmlentities(strip_tags($_GET["page"])) : "blog";

$content = retrieve_entries($link, $page, $url);
$is_full = array_pop($content);
$content = santinize_data($content);

if (!$content[0]) {
	echo "<p>", "该文章不存在。", "</p>";
} else {
	if ($is_full) {
		echo "<h2>", $content[0]["title"], "</h2>";
		if (isset($content[0]['page']))
			echo format_image($content[0]["image"], $content[0]["title"]);
		echo "<p>", $content[0]["entry"], "</p>";

		if (isset($content[0]['page'])) {
			// 只有登录后才能显示编辑内容
			if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] == 1) {
				echo "<p>";
				$admin = admin_link($content[0]['page'], $content[0]['url']);
				if ($page == 'blog') {
					echo $admin['edit'];
					echo " ";
				}
				echo $admin['delete'];
				echo "</p>";
			}
			// 显示评论框
			$comment = new Comments();
			$comments = $comment->show_comments($content[0]["id"]);
			$comments_form = $comment->show_comment_form($content[0]["id"]);
		} else {
			$comments = NULL;
			$comments_form = NULL;
		}
		echo "<h3>文章评论</h3>";
		echo $comments;
		echo $comments_form;

		if ($page != "about") {
			echo '<p class="backlink"><a href="/SimpleBlog/'."$page\"".
			'>回到文章列表</a></p>';
		}
	} else {
		foreach ($content[0] as $val) {
			// 两次编码, 一次送给阿帕奇
			// 一次给浏览器
			// 浏览器地址栏输入中文将会自动编码
			// 似乎在href里直接输入也是这样
			// 隐含的urlencode (现代浏览器才有)
			// 到 GET 时就变成UTF-8
			echo '<p><a href="/SimpleBlog/'.$val["page"]."/".
			urlencode(urlencode($val["url"])).
			'">'.$val["title"]."</a></p>";
		}
	}

}
?>


<?php if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] == 1):?>
<p class="backlink">
<a href="/SimpleBlog/admin/<?php echo $page?>">发布新文章</a>
</p>
<?php endif;?>

<p>
<a href="/SimpleBlog/feeds/rss.php">订阅RSS</a>
</p>
</div>

</body>
</html>